package com.openxu.calendar;

import android.content.Context;
import android.graphics.Rect;
import android.graphics.drawable.ColorDrawable;
import android.os.Build;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.PopupWindow;
import android.widget.TextView;

import java.util.List;

//日历选择年份
public class SelectYearPopupWindow extends PopupWindow
{

    private Context context;
    private List<String> data;
    private String time;
    private View.OnClickListener onClickListener;

    public SelectYearPopupWindow(Context context, List<String> data, View.OnClickListener onClickListener)
    {
        this.context = context;
        this.data = data;
        this.onClickListener = onClickListener;
        time = data.get(0);
        initView();
    }

    //适配7.0以上showAsDropDown方法无效
    @Override
    public void showAsDropDown(View anchor)
    {
        if (Build.VERSION.SDK_INT == 24)
        {
            Rect rect = new Rect();
            anchor.getGlobalVisibleRect(rect);
            int h = anchor.getResources().getDisplayMetrics().heightPixels - rect.bottom;
            setHeight(h);
        }
        super.showAsDropDown(anchor);
    }

    private void initView()
    {
        View view = LayoutInflater.from(context).inflate(R.layout.pop_select_year, null);
        setContentView(view);
        this.setWidth(ViewGroup.LayoutParams.FILL_PARENT);
        this.setHeight(ViewGroup.LayoutParams.FILL_PARENT);
        this.setFocusable(true);
        WheelView wheel_view_wv = (WheelView) view.findViewById(R.id.wheel_view_wv);
        TextView tv_pop_cancel = (TextView) view.findViewById(R.id.tv_pop_cancel);
        TextView tv_pop_complate = (TextView) view.findViewById(R.id.tv_pop_complate);
        ColorDrawable dw = new ColorDrawable(0xb0000000);
        //设置SelectPicPopupWindow弹出窗体的背景
        this.setBackgroundDrawable(dw);
        wheel_view_wv.setOnWheelViewListener(new WheelView.OnWheelViewListener()
        {
            @Override
            public void onSelected(int selectedIndex, String item)
            {
                time = item;
            }
        });
        wheel_view_wv.setOffset(2);
        wheel_view_wv.setItems(data);
        wheel_view_wv.setSeletion(0);
        tv_pop_cancel.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view)
            {
                dismiss();
            }
        });
        tv_pop_complate.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view)
            {
                view.setTag(Integer.parseInt(time));
                onClickListener.onClick(view);
                dismiss();
            }
        });
    }
}
